*Source: https://altair-viz.github.io/altair-tutorial/README.html
mark, a type of marker where you can then specify various configurations of x, y, color, and interactivityTypes of marks:
Illustrating example with WHO data on country-year-level life expectancy
import pandas as pd
import numpy as np
import altair as alt
from altair import datum
who = pd.read_csv('Life Expectancy Data.csv')
who.head()
who.columns = [col.strip().lower() for col in
who.columns]
who.columns
who.year.value_counts()
2013 193 2015 183 2014 183 2012 183 2011 183 2010 183 2009 183 2008 183 2007 183 2006 183 2005 183 2004 183 2003 183 2002 183 2001 183 2000 183 Name: year, dtype: int64
alt.Chart(who).mark_point()
alt.Chart(who[who.year == 2013]).mark_point().encode(
x = 'schooling',
y = 'life expectancy'
)
alt.Chart(who[who.year == 2013]).mark_point().encode(
x = 'schooling',
y = 'life expectancy',
color = 'status'
).configure_axis(
grid=False
)
encode() from:x = alt.X(), y = alt.Y(), color = alt.Color() with the parentheses then holding further customizations domain = ['Developing', 'Developed']
colors = ['seagreen', '#7D3C98']
alt.Chart(who[who.year == 2013]).mark_point().encode(
x = 'schooling',
y = 'life expectancy',
color = alt.Color('status').scale(domain = domain, range = colors)
).configure_axis(
grid=False
)
alt.Chart(who[who.year == 2013]).mark_point().encode(
x = alt.X('schooling', title = "Average years of schooling"),
y = alt.Y('life expectancy', title = "Life expectancy (2013)"),
color = alt.Color('status').scale(domain = domain, range = colors)
).configure_axis(
grid=False
)
mark_bar()¶who_subset = who[(who.country.isin(['United States of America', 'Canada', 'Mexico'])) &
(who.year > 2004)].copy()
alt.Chart(who_subset).mark_bar().encode(
x = alt.X('year', title = "Year"),
y = alt.Y('life expectancy', title = "Life expectancy"),
xOffset="country:N",
color = alt.Color('country:N', title = "")
)
alt.Chart(who_subset).mark_bar().encode(
x = alt.X('year:O', title = "Year"),
y = alt.Y('life expectancy:Q', title = "Life expectancy"),
xOffset="country:N",
color = alt.Color('country:N', title = "")
)
alt.Chart(who[who.year > 2009]).mark_bar().encode(
x = alt.X('year:O', title = "Year"),
xOffset = "status:N",
y = alt.Y('avg_life:Q', title = "Average Life expectancy"),
color = alt.Color('status:N', title = "")
).transform_aggregate(
avg_life = 'mean(life expectancy)',
groupby = ['status', 'year']
)
## notice layering of filters
alt.Chart(who).mark_bar().encode(
x = alt.X('year:O', title = "Year"),
y = alt.Y('life expectancy:Q', title = "Life expectancy"),
xOffset="country:N",
color = alt.Color('country:N', title = "")
).transform_filter(
alt.FieldOneOfPredicate(field = 'country',
oneOf = ["Canada", "Mexico",
"United States of America",
"Cuba"])
).transform_filter(
alt.FieldGTPredicate(field = 'year', gt = 2009)
)
- Allow users to select an interval range of the chart
- Allow users to select a single point
- Allow users to select multiple points
c = alt.Chart(who[who.year == 2013]).mark_point().encode(
x = alt.X('schooling', title = "Average years of schooling"),
y = alt.Y('life expectancy', title = "Life expectancy (2013)"),
color = alt.Color('status').scale(domain = domain, range = colors),
tooltip = [alt.Tooltip('country', title = "Country:"),
alt.Tooltip('life expectancy', title = "Life exp:"),
alt.Tooltip('schooling', title = 'Years schooling:')]
).configure_axis(
grid=False
).interactive()
add_selection() set of commands to select a certain region of pointsbrush = alt.selection_interval()
alt.Chart(who[who.year == 2013]).mark_point().encode(
x = alt.X('schooling', title = "Average years of schooling"),
y = alt.Y('life expectancy', title = "Life expectancy (2013)"),
color = alt.Color('status').scale(domain = domain, range = colors)
).configure_axis(
grid=False
).add_selection(
brush
)
/var/folders/mb/h311n7mj5dl4l2h43n8bnzs00000gp/T/ipykernel_43979/2472105602.py:2: AltairDeprecationWarning: Deprecated in `altair=5.0.0`. Use add_params instead. alt.Chart(who[who.year == 2013]).mark_point().encode(
brush = alt.selection_interval()
alt.Chart(who[who.year == 2013]).mark_point().encode(
x = alt.X('schooling', title = "Average years of schooling"),
y = alt.Y('life expectancy', title = "Life expectancy (2013)"),
color = alt.condition(brush, 'status:N', alt.value('lightgray'))
).configure_axis(
grid=False
).add_selection(
brush
)
/var/folders/mb/h311n7mj5dl4l2h43n8bnzs00000gp/T/ipykernel_43979/3208535782.py:2: AltairDeprecationWarning: Deprecated in `altair=5.0.0`. Use add_params instead. alt.Chart(who[who.year == 2013]).mark_point().encode(
transform_filter() that we outlined earlierscatter_schooling = alt.Chart(who[who.year == 2013]).mark_point().encode(
x = alt.X('schooling', title = "Average years of schooling"),
y = alt.Y('life expectancy', title = "Life expectancy (2013)"),
color = alt.Color('status').scale(domain = domain, range = colors))
scatter_gdp = alt.Chart(who[who.year == 2013]).mark_point().encode(
x = alt.X('gdp', title = "GDP"),
y = alt.Y('life expectancy', title = "Life expectancy (2013)"),
color = alt.Color('status').scale(domain = domain, range = colors))
scatter_schooling | scatter_gdp
brush = alt.selection_interval()
scatter_schooling = alt.Chart(who[who.year == 2013]).mark_point().encode(
x = alt.X('schooling', title = "Average years of schooling"),
y = alt.Y('life expectancy', title = "Life expectancy (2013)"),
color = alt.condition(brush, 'status:N', alt.value('lightgray'))
).add_selection(
brush
)
scatter_gdp = alt.Chart(who[who.year == 2013]).mark_point().encode(
x = alt.X('gdp', title = "GDP"),
y = alt.Y('life expectancy', title = "Life expectancy (2013)"),
color = 'status:N').transform_filter(
brush
)
scatter_schooling | scatter_gdp
/var/folders/mb/h311n7mj5dl4l2h43n8bnzs00000gp/T/ipykernel_43979/1044852389.py:2: AltairDeprecationWarning: Deprecated in `altair=5.0.0`. Use add_params instead. scatter_schooling = alt.Chart(who[who.year == 2013]).mark_point().encode(
select_year = alt.selection_interval(encodings = ['x'])
bar_slider = alt.Chart(who).mark_bar().encode(
x = 'year:O',
y = 'count()'
).add_selection(select_year)
scatter_schooling = alt.Chart(who).mark_point().encode(
x = alt.X('schooling', title = "Average years of schooling"),
y = alt.Y('life expectancy', title = "Life expectancy (2013)"),
color = alt.condition(select_year, 'status:N', alt.value('lightgray')),
opacity = alt.condition(select_year, alt.value(0.8), alt.value(0.1))
)
scatter_schooling & bar_slider
/var/folders/mb/h311n7mj5dl4l2h43n8bnzs00000gp/T/ipykernel_43979/1050406603.py:3: AltairDeprecationWarning: Deprecated in `altair=5.0.0`. Use add_params instead. bar_slider = alt.Chart(who).mark_bar().encode(
mark as the basic building blockencode to specify mappings to x, y, and colortransform_aggregate and transform_filter to transform data within the plotting call itself